using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
    [Serializable()]
    public partial class CLabel_OneToOneList : List<CLabel_OneToOne>
    {
        #region Constructors
        //Basic constructor
        public CLabel_OneToOneList() : base() {}
       
        //More efficient memory-allocation if size is known
        public CLabel_OneToOneList(int capacity) : base(capacity) {}
        
        //Shares the index (if its already been computed)
        public CLabel_OneToOneList(CLabel_OneToOneList list) : base(list.Count)
        {
            foreach (CLabel_OneToOne i in list)
                base.Add(i);
        }

        //Generic list (eg. from paging control), have to assume type
        public CLabel_OneToOneList(IList list) : base(list.Count)
        {
            foreach (CLabel_OneToOne i in list)
                base.Add(i);
        }
        #endregion

        #region Top/Bottom/Page
        public CLabel_OneToOneList Top(int count)
        {
            if (count >= this.Count)
                return this;
            return Page(count, 0); 
        }
        public CLabel_OneToOneList Bottom(int count)   
        {
            if (count > this.Count)
                count = this.Count;
            return new CLabel_OneToOneList(this.GetRange(this.Count - count - 1, count));
        }
        public CLabel_OneToOneList Page(int pageSize, int pageIndex)
        {
            return new CLabel_OneToOneList( CUtilities.Page(this, pageSize, pageIndex) );
        }
        #endregion

        #region BulkEditLogic
        public bool HaveSameValue(string propertyName)               { return CReflection.HaveSameValue(this, propertyName); }
        public void SetSameValue( string propertyName, object value) {        CReflection.SetSameValue( this, propertyName, value); }
        #endregion

        #region SortBy
        //Public
        public CLabel_OneToOneList SortBy(string propertyName) { return SortBy(propertyName, false); }
        public CLabel_OneToOneList SortBy(string propertyName, bool descending)
        {
            CLabel_OneToOneList copy = new CLabel_OneToOneList(this);
            if (this.Count == 0)    return copy;
            copy.Sort(new CLabel_OneToOneList_SortBy(propertyName, descending, this));
            return copy;
        }
        //Private 
        private class CLabel_OneToOneList_SortBy : CReflection.GenericSortBy, IComparer<CLabel_OneToOne>
        {
            public CLabel_OneToOneList_SortBy(string propertyName, bool descending, IList list) : base(propertyName, descending, list) { }
            public int Compare(CLabel_OneToOne x, CLabel_OneToOne y) { return base.Compare(x, y); }
        }
        #endregion

        #region SaveAll/DeleteAll
        //Use default connection (may be overridden in base class)
        public void SaveAll()   {   if (this.Count > 0) { SaveAll(  this[0].DataSrc);    }    }
        public void DeleteAll() {   if (this.Count > 0) { DeleteAll(this[0].DataSrc);    }    }

        //Use connection supplied
        public void SaveAll(  CDataSrc dataSrc) {   dataSrc.BulkSave(this);    }
        public void DeleteAll(CDataSrc dataSrc) {   dataSrc.BulkDelete(this);  }

        //Use transaction supplied
        public void SaveAll(  IDbTransaction txOrNull)    {   foreach (CLabel_OneToOne i in this) {   i.Save(  txOrNull);   }   }
        public void DeleteAll(IDbTransaction txOrNull)    {   foreach (CLabel_OneToOne i in this) {   i.Delete(txOrNull);   }   }

        //Use a specified isolation level
        public void SaveAll(  IsolationLevel txIsolationLevel)   {   if (this.Count > 0) { SaveAll(  this[0].DataSrc, txIsolationLevel);  }    }
        public void DeleteAll(IsolationLevel txIsolationLevel)   {   if (this.Count > 0) { DeleteAll(this[0].DataSrc, txIsolationLevel);  }    }

        //Use a specified connection and isolation level
        public void SaveAll(  CDataSrc dataSrc, IsolationLevel txIsolationLevel)   {   dataSrc.BulkSave(  this, txIsolationLevel);  }
        public void DeleteAll(CDataSrc dataSrc, IsolationLevel txIsolationLevel)   {   dataSrc.BulkDelete(this, txIsolationLevel);  }
        #endregion
        
        #region Cache-Control
    
        //Supplementary List Overloads
        public void Add(   IList<CLabel_OneToOne> itemsToAdd)    {   foreach (CLabel_OneToOne i in itemsToAdd)   { Add(   i); }   }
        public void Remove(IList<CLabel_OneToOne> itemsToRemove) {   foreach (CLabel_OneToOne i in itemsToRemove){ Remove(i); }   }
        #endregion  

        #region Main Index (on LabelGuid, LabelTypeId)
        public CLabel_OneToOne GetById(Guid labelGuid, int labelTypeId)
        {
            CLabel_OneToOne c = null;
            string k = Key(labelGuid, labelTypeId);
            Index.TryGetValue(k, out c);
            return c;
        }
        public static string Key(Guid labelGuid, int labelTypeId)
        {
            return String.Concat(labelGuid, "_", labelTypeId);
        }

        [NonSerialized]
        private Dictionary<string,CLabel_OneToOne> _index;
        private Dictionary<string,CLabel_OneToOne> Index
        {
            get    
            {
                if (null != _index)
                    if (_index.Count == this.Count)
                        return _index;

                _index = new Dictionary<string,CLabel_OneToOne>(this.Count);
                foreach (CLabel_OneToOne i in this)
                    _index[ Key(i.LabelGuid, i.LabelTypeId) ] = i;
                return _index;
             }
        }
        #endregion
            
        #region Foreign-Key Indices (Subsets)
        //Index by LabelTagId
        public CLabel_OneToOneList GetByTagId(int tagId)
        {
            CLabel_OneToOneList temp = null;
            if (! IndexByTagId.TryGetValue(tagId, out temp))
            {
                temp = new CLabel_OneToOneList();
                IndexByTagId[tagId] = temp;
            }
            return temp;
        }

        [NonSerialized]
        private Dictionary<int, CLabel_OneToOneList> _indexByTagId;
        private Dictionary<int, CLabel_OneToOneList> IndexByTagId
        {
            get
            {
                if (null == _indexByTagId)
                {
                    Dictionary<int, CLabel_OneToOneList> index = new Dictionary<int, CLabel_OneToOneList>();
                    CLabel_OneToOneList temp = null;
                    foreach (CLabel_OneToOne i in this)
                    {
                        if (! index.TryGetValue(i.LabelTagId, out temp))
                        {
                            temp = new CLabel_OneToOneList();
                            index[i.LabelTagId] = temp;
                        }
                        temp.Add(i);
                    }
                    _indexByTagId = index;
                }
                return _indexByTagId;
            }
        }
        //Index by LabelGuid
        public CLabel_OneToOneList GetByGuid(Guid guid)
        {
            CLabel_OneToOneList temp = null;
            if (! IndexByGuid.TryGetValue(guid, out temp))
            {
                temp = new CLabel_OneToOneList();
                IndexByGuid[guid] = temp;
            }
            return temp;
        }

        [NonSerialized]
        private Dictionary<Guid, CLabel_OneToOneList> _indexByGuid;
        private Dictionary<Guid, CLabel_OneToOneList> IndexByGuid
        {
            get
            {
                if (null == _indexByGuid)
                {
                    Dictionary<Guid, CLabel_OneToOneList> index = new Dictionary<Guid, CLabel_OneToOneList>();
                    CLabel_OneToOneList temp = null;
                    foreach (CLabel_OneToOne i in this)
                    {
                        if (! index.TryGetValue(i.LabelGuid, out temp))
                        {
                            temp = new CLabel_OneToOneList();
                            index[i.LabelGuid] = temp;
                        }
                        temp.Add(i);
                    }
                    _indexByGuid = index;
                }
                return _indexByGuid;
            }
        }
        //Index by LabelTypeId
        public CLabel_OneToOneList GetByTypeId(int typeId)
        {
            CLabel_OneToOneList temp = null;
            if (! IndexByTypeId.TryGetValue(typeId, out temp))
            {
                temp = new CLabel_OneToOneList();
                IndexByTypeId[typeId] = temp;
            }
            return temp;
        }

        [NonSerialized]
        private Dictionary<int, CLabel_OneToOneList> _indexByTypeId;
        private Dictionary<int, CLabel_OneToOneList> IndexByTypeId
        {
            get
            {
                if (null == _indexByTypeId)
                {
                    Dictionary<int, CLabel_OneToOneList> index = new Dictionary<int, CLabel_OneToOneList>();
                    CLabel_OneToOneList temp = null;
                    foreach (CLabel_OneToOne i in this)
                    {
                        if (! index.TryGetValue(i.LabelTypeId, out temp))
                        {
                            temp = new CLabel_OneToOneList();
                            index[i.LabelTypeId] = temp;
                        }
                        temp.Add(i);
                    }
                    _indexByTypeId = index;
                }
                return _indexByTypeId;
            }
        }

        #endregion


    }
}